package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.config.MyRedisTemplate;
import com.woniu.car.domain.TShopAccount;
import com.woniu.car.dto.ShopAccountDto;
import com.woniu.car.dto.ShopRegisterDto;
import com.woniu.car.exception.AccountException;
import com.woniu.car.mapper.TShopAccountMapper;
import com.woniu.car.param.ShopLoginByCodeParam;
import com.woniu.car.param.ShopRegisterParam;
import com.woniu.car.service.TShopAccountService;
import com.woniu.car.util.AccountStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Service
public class TShopAccountServiceImpl extends ServiceImpl<TShopAccountMapper, TShopAccount> implements TShopAccountService {
    @Resource
    private MyRedisTemplate myRedisTemplate;

    /**
     *@Author 王维鑫
     *@Description 新增商铺账号
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public TShopAccount insertShopAccount(TShopAccount tShopAccount)  {
        boolean save = this.save(tShopAccount);
        if(save==false){
            throw new AccountException("新增商铺账号失败");
        }
        return tShopAccount;
    }
    /**
     *@Author 王维鑫
     *@Description 修改商铺账号
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public void updateShopAccount(TShopAccount tShopAccount)  {
        boolean b = this.updateById(tShopAccount);
        if(b==false){
            throw new AccountException("修改商铺账号失败");
        }
    }
    /**
     *@Author 王维鑫
     *@Description 查看商铺账号列表
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TShopAccount> showShopAccount() {
        List<TShopAccount> list = this.list();
        return list;
    }
    /**
     *@Author 王维鑫
     *@Description 查询商铺账号
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TShopAccount> queryShopAccount(TShopAccount tShopAccount) {
        QueryWrapper<TShopAccount> wrapper = new QueryWrapper<>();
        if(null!=tShopAccount.getShopAccountId() ){
            wrapper.eq("shop_account_id",tShopAccount.getShopAccountId());
        }
        if(null!=tShopAccount.getShopId() ){
            wrapper.eq("shop_id",tShopAccount.getShopId());
        }
        if(null!=tShopAccount.getShopStatus()){
            wrapper.eq("shop_status",tShopAccount.getShopStatus());
        }
        if(null!=tShopAccount.getShopTel()){
            wrapper.eq("shop_tel",tShopAccount.getShopTel());
        }
        List<TShopAccount> list = this.list(wrapper);
        return list;
    }
    /**
     *@Author 王维鑫
     *@Description 商铺注册
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public ShopRegisterDto registerShopAccount(ShopRegisterParam param)  {
                Date date = new Date();
                long time = date.getTime();
                TShopAccount tShopAccount = new TShopAccount();
        tShopAccount.setShopTel(param.getShopTel());
        List<TShopAccount> tShopAccounts = this.queryShopAccount(tShopAccount);
        if(tShopAccounts.size()==0){
            tShopAccount.setShopId(param.getShopId());
           // BeanCopyUtils.copyProperties(param,tShopAccount);
            tShopAccount.setRegistTime(time);
            tShopAccount.setShopStatus(AccountStatus.NORMAL);
            tShopAccount.setShopPwd(param.getShopPwd());
            TShopAccount tShopAccount1 = this.insertShopAccount(tShopAccount);
            ShopRegisterDto shopAccountDto = new ShopRegisterDto();
            BeanCopyUtils.copyProperties(tShopAccount1,shopAccountDto);
            return shopAccountDto;
        }else {
            throw new AccountException("该手机号已注册，请更换");
        }

    }

    /**
     *@Author 王维鑫
     *@Description 短信登录
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public ShopAccountDto loginShopByCode(ShopLoginByCodeParam param)  {
            TShopAccount tShopAccount = new TShopAccount();
            tShopAccount.setShopTel(param.getShopTel());
            List<TShopAccount> tShopAccounts = this.queryShopAccount(tShopAccount);
            if(tShopAccounts.get(0)==null){
                throw new AccountException("该账号未注册");
            }else {
                ShopAccountDto shopAccountDto = new ShopAccountDto();
                BeanCopyUtils.copyProperties(tShopAccounts.get(0),shopAccountDto);
                return shopAccountDto;
            }
    }
}
